﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LoaderManager : MonoBehaviour {
	
	public enum LoadTypeEnum
	{
		Window,
		Font,
		Atlas,
		Material,
		Effect,
		Sound,
		XML,
	}
	
	Dictionary<string, UIResourceInfo> windowDictionary = new Dictionary<string, UIResourceInfo>();
	
	static LoaderManager instance;
	
	public static LoaderManager GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;
	}
	
	public void Init()
	{
		
	}
	
	public void InitLoadXML(string url, LoadTypeEnum type)
	{
		LoadXML.GetInstance().InitLoadXml(url,
			this, this.GetType().GetMethod("LoadXmlOver"), type);
	}
	
	public void LoadXmlOver(object obj, object type)
	{
		Dictionary<string, UIResourceInfo> list = obj as Dictionary<string, UIResourceInfo>;
		
		LoadTypeEnum loadType = (LoadTypeEnum) type;
		
		windowDictionary = list;
		
		foreach(KeyValuePair<string, UIResourceInfo> v in list)
		{
			LoadAssetBunlde(v.Value.localPath, loadType);
		}
	}
	
	public void LoadAssetBunlde(string url, LoadTypeEnum load)
	{
		switch(load)
		{
		case LoadTypeEnum.Atlas:
			TObjectManager.GetInstances().Load(null, url, OnLoadOverAtlas);
			break;
		case LoadTypeEnum.Effect:
			TObjectManager.GetInstances().Load(null, url, OnLoadOverEffect);
			break;
		case LoadTypeEnum.Font:
			TObjectManager.GetInstances().Load(null, url, OnLoadOverFont);
			break;
		case LoadTypeEnum.Material:
			TObjectManager.GetInstances().Load(null, url, OnLoadOverMaterial);
			break;
		case LoadTypeEnum.Sound:
			TObjectManager.GetInstances().Load(null, url, OnLoadOverSound);
			break;
		case LoadTypeEnum.Window:
			TObjectManager.GetInstances().Load(UIManager.windowRoot, url, OnLoadOverWindow);
			break;
		default:
			break;
		}
	}
	
	void OnLoadOverWindow(object obj, TMyFile www)
	{
		if(www == null)
			return;
		if(obj == null)
			return;
		
		if(windowDictionary.ContainsKey(www.url))
			windowDictionary.Remove(www.url);
		
		Transform trans = obj as Transform;
		GameObject go = Instantiate(www.obj) as GameObject;
		
		go.SetActiveRecursively(true);
		
		go.name = go.name.Replace("(Clone)", "");
		
		UIManager.InstantiateWindow(go);
	}
	
	void OnLoadOverFont(object obj, TMyFile www)
	{
		if(www == null)
			return;
		
		UIManager.FontAsset fontInfo = new UIManager.FontAsset();
		fontInfo.filePath = www.url;
		fontInfo.font = (www.obj as GameObject).GetComponent<UIFont>();
		UIManager.SetFont(fontInfo);
	}
	
	void OnLoadOverAtlas(object obj, TMyFile www)
	{
		if(www == null)
			return;
		
		UIManager.AtlasAsset assetInfo = new UIManager.AtlasAsset();
		assetInfo.filePath = www.url;
		assetInfo.atlas = (www.obj as GameObject).GetComponent<UIAtlas>();
		UIManager.SetAtlas(assetInfo);
	}
	
	void OnLoadOverMaterial(object obj, TMyFile www)
	{
		if(www == null)
			return;
		UIManager.MaterialAsset assetInfo = new UIManager.MaterialAsset();
		assetInfo.filePath = www.url;
		assetInfo.material = www.obj as Material;
		UIManager.SetMaterial(assetInfo);
	}
	
	void OnLoadOverEffect(object obj, TMyFile www)
	{
		
	}
	
	void OnLoadOverSound(object obj, TMyFile www)
	{
		if(www == null)
			return;
		UIManager.SoundAsset assetInfo = new UIManager.SoundAsset();
		assetInfo.filePath = www.url;
		assetInfo.audioClip = www.obj as AudioClip;
		UIManager.SetSound(assetInfo);
	}
	
	/// <summary>
	/// 窗口资源是否已经加载完毕
	/// </summary>
	public bool IsLoadOverWindow()
	{
		return windowDictionary.Count == 0;
	}
}
